package com.pucheng.bracelet.ui.activity.upgrade

import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.lifecycle.observe
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.ble.ClientManager
import com.pucheng.bracelet.app.ble.DfuUtils
import com.pucheng.bracelet.app.util.click
import com.pucheng.bracelet.databinding.ActivityMsgPushBinding
import com.pucheng.bracelet.databinding.ActivityUpgradeBinding
import com.pucheng.bracelet.ui.adapter.PushAdapter
import com.pucheng.bracelet.ui.adapter.PushData
import com.pucheng.bracelet.viewmodel.UpgradeViewModel
import com.pucheng.bracelet.viewmodel.meaasge.MsgPushViewModel
import kotlinx.android.synthetic.main.activity_upgrade.*
import kotlinx.android.synthetic.main.layout_title.*
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.demo.app.base.BaseActivity
import no.nordicsemi.android.dfu.DfuProgressListener
import java.io.File

class UpgradeActivity : BaseActivity<UpgradeViewModel, ActivityUpgradeBinding>() {
    private var animtorAlpha: ObjectAnimator? = null
    override fun layoutId(): Int = R.layout.activity_upgrade

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.apply {
            animtorAlpha = ObjectAnimator.ofFloat(ivUpgrade, "rotation", 0f, 360f)
            viewModel = mViewModel
            lifecycleOwner = this@UpgradeActivity
            ibtn_back.apply {
                setOnClickListener {
                    finish()
                }
            }
            tv_title.apply {
                text = "固件升级"
            }
            tvUpgrade.click {
                tvVersionTips.text = "更新中..."
                animtorAlpha?.apply {
                    startRotation( this)
                }
                mViewModel.newFirmware.value = false
                mViewModel.upgrade(this@UpgradeActivity)
            }

        }
        mViewModel.apply {

        }
    }

    override fun createObserver() {
    }

    override fun dismissLoading() {
    }

    override fun showLoading(message: String) {

    }

    override fun finish() {
        super.finish()
        animtorAlpha?.apply {
            stopRotation( this)
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        animtorAlpha?.apply {
            stopRotation( this)
        }
    }

    private fun startRotation(animtorAlpha: ObjectAnimator) {
        animtorAlpha.interpolator = LinearInterpolator()
        animtorAlpha.repeatCount = 100;
        animtorAlpha.duration = 1000;
        animtorAlpha.start();
    }

    private fun stopRotation(animtorAlpha: ObjectAnimator) {
        animtorAlpha.end();
    }
}